import { i18n, setHtmlPageLang } from '@/plugins/vueI18n';
import { useLocaleStoreWithOut } from '@/store/modules/locale';

const setI18nLanguage = (locale: LocaleType) => {
  const localeStore = useLocaleStoreWithOut();

  (i18n.global.locale as any).value = locale;
  localeStore.setCurrentLocale({ lang: locale });
  setHtmlPageLang(locale);
};

export const useLocale = () => {
  /**
   * 切换当前语言
   * @param {LocaleType} newLocal 需需要切换的语言
   */
  const changeLocale = async (newLocal: LocaleType) => {
    const globalI18n = i18n.global;
    const langModule = await import(`@/locales/${newLocal}.yaml`);
    globalI18n.setLocaleMessage(newLocal, langModule.default);
    setI18nLanguage(newLocal);
  };

  return {
    changeLocale
  };
};
